package com.hr.ping

import com.hr.ping.util.FileLockUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification

import java.nio.file.Paths

@SpringBootTest(classes = PingApplication)
@ActiveProfiles("dev")
class FileLockUtilTest extends Specification {

    @Autowired
    FileLockUtil fileLockUtil

    def "acquireRateLimitLock should acquire and release lock"() {
        when:
        def lock1 = fileLockUtil.acquireRateLimitLock().block()
        def lock2 = fileLockUtil.acquireRateLimitLock().block()

        then:
        lock1 != null
        lock2 == null

        cleanup:
        lock1.release()
    }

    def "acquireRateLimitLock should handle exceptions"() {
        given:
        fileLockUtil.lockFilePath = Paths.get("nonexistent_directory/lockfile.lock")

        when:
        fileLockUtil.acquireRateLimitLock().block()

        then:
        thrown RuntimeException
    }
}
